<!DOCTYPE html>
<title>SVGTextContentElement methods' parameters are correctly validated</title>
<link rel="help" href="http://www.w3.org/TR/SVG2/text.html#InterfaceSVGTextContentElement">
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
test(function() {
  var svgNS = "http://www.w3.org/2000/svg";
  var svgRoot = document.createElementNS(svgNS, "svg");
  document.documentElement.appendChild(svgRoot);

  var svgText = document.createElementNS(svgNS, "text");
  svgText.style.fontFamily = "Ahem";
  svgText.style.fontSize = "20px";
  svgText.appendChild(document.createTextNode("abcdefg"));
  svgRoot.appendChild(svgText);

  var emptySvgText = document.createElementNS(svgNS, "text");
  svgRoot.appendChild(emptySvgText);

  function assert_equals_to_SVGPoint(actualPoint, expectedPoint)
  {
    assert_equals(actualPoint.x, expectedPoint.x);
    assert_equals(actualPoint.y, expectedPoint.y);
  }

  function assert_equals_to_SVGRect(actualRect, expectedRect)
  {
    assert_equals(actualRect.x, expectedRect.x);
    assert_equals(actualRect.y, expectedRect.y);
    assert_equals(actualRect.width, expectedRect.width);
    assert_equals(actualRect.height, expectedRect.height);
  }

  // Arguments should be mandatory.
  assert_throws_js(TypeError, function() { svgText.getSubStringLength(); });
  assert_throws_js(TypeError, function() { svgText.getSubStringLength(2); });
  assert_throws_js(TypeError, function() { svgText.getStartPositionOfChar(); });
  assert_throws_js(TypeError, function() { svgText.getEndPositionOfChar(); });
  assert_throws_js(TypeError, function() { svgText.getExtentOfChar(); });
  assert_throws_js(TypeError, function() { svgText.getRotationOfChar(); });
  assert_throws_js(TypeError, function() { svgText.getCharNumAtPosition(); });
  assert_throws_js(TypeError, function() { svgText.getCharNumAtPosition('aString'); });
  assert_throws_js(TypeError, function() { svgText.getCharNumAtPosition(svgText); });
  assert_throws_js(TypeError, function() { svgText.selectSubString(); });
  assert_throws_js(TypeError, function() { svgText.selectSubString(2); });

  // Should throw an IndexSizeError if charnum is greater than or equal to the number of characters at this node.
  assert_throws_dom("IndexSizeError", function() { svgText.getSubStringLength(999, 2); });
  assert_throws_dom("IndexSizeError", function() { svgText.getStartPositionOfChar(999); });
  assert_throws_dom("IndexSizeError", function() { svgText.getEndPositionOfChar(999); });
  assert_throws_dom("IndexSizeError", function() { svgText.getExtentOfChar(999); });
  assert_throws_dom("IndexSizeError", function() { svgText.getRotationOfChar(999); });
  assert_throws_dom("IndexSizeError", function() { svgText.selectSubString(999, 2); });

  // Test the equality part of the restriction.
  assert_throws_dom("IndexSizeError", function() { svgText.getSubStringLength(7, 2); });
  assert_throws_dom("IndexSizeError", function() { svgText.getStartPositionOfChar(7); });
  assert_throws_dom("IndexSizeError", function() { svgText.getEndPositionOfChar(7); });
  assert_throws_dom("IndexSizeError", function() { svgText.getExtentOfChar(7); });
  assert_throws_dom("IndexSizeError", function() { svgText.getRotationOfChar(7); });
  assert_throws_dom("IndexSizeError", function() { svgText.selectSubString(7, 2); });

  // Test the equality part of the restriction for the <number of chars> == 0 case.
  assert_throws_dom("IndexSizeError", function() { emptySvgText.getSubStringLength(0, 2); });
  assert_throws_dom("IndexSizeError", function() { emptySvgText.getStartPositionOfChar(0); });
  assert_throws_dom("IndexSizeError", function() { emptySvgText.getEndPositionOfChar(0); });
  assert_throws_dom("IndexSizeError", function() { emptySvgText.getExtentOfChar(0); });
  assert_throws_dom("IndexSizeError", function() { emptySvgText.getRotationOfChar(0); });
  assert_throws_dom("IndexSizeError", function() { emptySvgText.selectSubString(0, 2); });

  // Should not throw if charnum is negative and wraps to a valid positive index (-4294967294 wraps to 2).
  assert_equals(svgText.getSubStringLength(-4294967294, 2), svgText.getSubStringLength(2, 2));
  assert_equals_to_SVGPoint(svgText.getStartPositionOfChar(-4294967294), svgText.getStartPositionOfChar(2));
  assert_equals_to_SVGPoint(svgText.getEndPositionOfChar(-4294967294), svgText.getEndPositionOfChar(2));
  assert_equals_to_SVGRect(svgText.getExtentOfChar(-4294967294), svgText.getExtentOfChar(2));
  assert_equals(svgText.getRotationOfChar(-4294967294), svgText.getRotationOfChar(2));
  svgText.selectSubString(-4294967294, 2);

  // Should throw an IndexSizeError if charnum is negative and wraps to an invalid positive index.
  assert_throws_dom("IndexSizeError", function() { svgText.getSubStringLength(-1, 2); });
  assert_throws_dom("IndexSizeError", function() { svgText.getStartPositionOfChar(-1); });
  assert_throws_dom("IndexSizeError", function() { svgText.getEndPositionOfChar(-1); });
  assert_throws_dom("IndexSizeError", function() { svgText.getExtentOfChar(-1); });
  assert_throws_dom("IndexSizeError", function() { svgText.getRotationOfChar(-1); });
  assert_throws_dom("IndexSizeError", function() { svgText.selectSubString(-1, 2); });

  // We should not throw if nchars is negative or too large.
  // If nchars specifies more characters than are available, then the substring will consist of all characters
  // starting with charnum until the end of the list of characters.
  assert_equals(svgText.getSubStringLength(2, 999), svgText.getSubStringLength(2, 5));
  assert_equals(svgText.getSubStringLength(2, -1), svgText.getSubStringLength(2, 5));
  assert_equals(svgText.getSubStringLength(2, 2), svgText.getSubStringLength(2, -4294967294));
  svgText.selectSubString(2, 999);
  svgText.selectSubString(2, -1);
  svgText.selectSubString(2, -4294967294);

  // cleanup
  document.documentElement.removeChild(svgRoot);
});
</script>
